package com.ysd.lis.util.qcrule;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static cn.hutool.poi.excel.sax.AttributeName.r;

public class RLRule extends Rule{
    public RLRule(String id, String name, int a, int n, double l) {
        super(id, name, a, n, l);
        this.type = RuleType.RLRule;
        this.a = 2;
    }

    @Override
    public RuleStatement Evaluate(QCResult result) {
        if (result.getResults().size() < a)
        {
            return RuleStatement.DISABLE;
        }
        List<Result> results = result.getResults();
        //var items = result.getResults().GetRange(result.Results.Count - A, A);
        List<Result> items = new ArrayList<>();
        for(int i = 0 ;i< a; i++){
            items.add(results.get(results.size()-a+i));
        }


        if (HasEmptyResult(items))
        {
            return RuleStatement.DISABLE;
        }

        if (Math.abs(items.get(0).getZvalue() - items.get(1).getZvalue()) > l)
        {
            items.get(items.size()-1).setIsOutControl(true);

            items.get(items.size()-1).getOutControlInfos().add(new OutControlInfo(id, false,
                    items.stream().map(Result::getId).collect(Collectors.toList()))
            );

            return RuleStatement.OK;
        }

        return RuleStatement.FAILED;
    }

    @Override
    public RuleStatement EvaluateMulti(List<QCResult> results) {
        //无数据,失败
        //if (!results.Any())
        if(CollectionUtils.isEmpty(results))
        {
            return RuleStatement.FAILED;
        }

        //每个质控品必须有一个数据
        //if ( results.Select(p => p.Results.Count).Min() < 1)
        List<QCResult> collect = results.stream().filter(a -> a.getResults().size() < 1).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(collect))
        {
            return RuleStatement.FAILED;
        }

        QCResult result = new QCResult();

        //获取必要数据
        for (QCResult r : results)
        {
            result.getResults().add(r.getResults().get(r.getResults().size()-1));
        }


        if (HasEmptyResult(result.getResults()))
        {
            return RuleStatement.DISABLE;
        }

        //
        for (int i = 0; i < result.getResults().size(); i++)
        {

            for (int j = 0; j < result.getResults().size(); j++)
            {
                if (i == j)
                {
                    continue;
                }


                if (Math.abs(result.getResults().get(i).getZvalue() - result.getResults().get(j).getZvalue()) > l)
                {
                    result.getResults().get(i).setIsOutControl(true);


                    result.getResults().get(i).getOutControlInfos().add(new OutControlInfo(id,false,
                             Arrays.asList(result.getResults().get(i).getId(), result.getResults().get(j).getId()) ));

                    result.getResults().get(j).setIsOutControl(true);

                    result.getResults().get(j).getOutControlInfos().add(new OutControlInfo(id,false,
                            Arrays.asList(result.getResults().get(i).getId(), result.getResults().get(j).getId()) ));
                }
            }
        }

        //if (result.Results.Count(p => (p.IsOutControl===true)) > 0)
        if(result.getResults().stream().filter(p->p.getIsOutControl()).count()>0)
        {
            syncOutControlInfo(results, result);

            return RuleStatement.OK;
        }

        return RuleStatement.FAILED;
    }
}
